import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.text.html.HTML;
import javax.swing.text.html.HTML.Tag;

public class espacenet extends IProvider {
	public espacenet(BotConfig cfg){
		super(cfg);
		_posCntSD = new SearchDetails("([0-9].*) results? found in the Worldwide database", 1, null, Tag.TD);
		_posDtlSD = new SearchDetails(null, -1, Tag.A, null, "^(/publicationDetails/.*)$", 1, HTML.Attribute.HREF);
		_nextSD = new SearchDetails("^\\s*next\\s*$", -1, Tag.A, null, "(.*)", 1, HTML.Attribute.HREF );
		_nxtDtlSD = new SearchDetails("^\\s*Original\\s*document\\s*$", -1, Tag.A, null, "(.*)", 1, HTML.Attribute.HREF );
		
		_keysSD = new SearchDetails[]{				
				new SearchDetails("^\\s*Publication number:\\s*(.*)\\s*$", 1, null, Tag.TR),
				new SearchDetails("^\\s*(.*)\\s*$", 1, null, Tag.H2),
				new SearchDetails("^\\s*Publication date:\\s*(.*)\\s*$", 1, null, Tag.TR),
				new SearchDetails("^\\s*(.*)\\s*$", 1, null, Tag.TD, "abCell", -1, HTML.Attribute.ID),
				new SearchDetails("^\\s*Save Full Document\\s*$", -1, Tag.A, null, "(.*)", 1, HTML.Attribute.HREF)
		};
		_keys = new String[]{				
				"Publication number",
				"Publication title",
				"Publication date",
				"Abstract",
				"Url"
		};
	}
	
	private SearchDetails _posCntSD = null;
	@Override
	protected SearchDetails getPositionCountSearch(){
		return _posCntSD;
	}
	
	private SearchDetails _nextSD = null;
	@Override
	protected SearchDetails getNextSearch(){
		return _nextSD;
	}
	
	private SearchDetails _posDtlSD = null;
	@Override
	protected SearchDetails getPositionDetailSearch(){
		return _posDtlSD;
	}
	
	private SearchDetails _nxtDtlSD = null;
	@Override
	protected SearchDetails getNextDetail(){
		return _nxtDtlSD;
	}
	
	@Override
	protected String getEnginName() {
		return "espacenet";
	}	
	@Override
	protected String getSearchURL(int Year, String useCase, String categ) {		
		String patUrl ="http://v3.espacenet.com/searchResults?DB=EPODOC&PD=" + Integer.toString(Year) +
		"&submitted=true&locale=en_EP&AB=" + categ;		
		if(useCase.length() > 0)
			patUrl += "+%28" + useCase.replaceAll("\\|", "+OR+") + "%29";
		patUrl += "&ST=advanced&compact=false";
		return patUrl;
	}
	
	Pattern urlPat = null;	
	@Override	
	protected String getURL(String extUrl){
		if(extUrl == null)
			return "";
		if(extUrl.contains("jsessionid"))
		{
			if(urlPat == null)
				urlPat = Pattern.compile("(.*);jsessionid=(.*)(\\?.*)");
			Matcher m = urlPat.matcher(extUrl);
			if(m.find()){
				extUrl = m.group(1) + m.group(3);
	    	}
		}
		return "http://v3.espacenet.com/" + extUrl;
	}
	
	SearchDetails[] _keysSD = null;
	String[] _keys = null;
	@Override
	protected SearchDetails getKeySearchDetails(int idx) {
		return _keysSD[idx];
	}
	@Override
	protected int getKeyWordCount() {
		return _keys.length;
	}
	@Override
	protected String getKeyWordName(int idx) {
		return _keys[idx];
	}
}
